package com.company;

import java.util.HashMap;
import java.util.Map;

// 主类
public class FruitStore {
    public static void main(String[] args) {
        // 顾客A：只有苹果和草莓，无折扣
        ShoppingCart cartA = new ShoppingCart(new NoChuXiao());
        cartA.addItem(new Apple(), 3);
        cartA.addItem(new Strawberry(1.0), 2);
        System.out.println("顾客A的消费: " + cartA.calculateTotal() + "元");

        // 顾客B：新增芒果，无折扣
        ShoppingCart cartB = new ShoppingCart(new NoChuXiao());
        cartB.addItem(new Apple(), 2);
        cartB.addItem(new Strawberry(1.0), 3);
        cartB.addItem(new Mango(), 1);
        System.out.println("顾客B的消费: " + cartB.calculateTotal() + "元");

        // 顾客C：草莓打8折
        ShoppingCart cartC = new ShoppingCart(new NoChuXiao());
        cartC.addItem(new Apple(), 2);
        cartC.addItem(new Strawberry(0.8), 4);
        cartC.addItem(new Mango(), 1);
        System.out.println("顾客C的消费: " + cartC.calculateTotal() + "元");

        // 顾客D：草莓打8折且满100减10
        ShoppingCart cartD = new ShoppingCart(new FullDiscountChuXiao(100, 10));
        cartD.addItem(new Apple(), 5);
        cartD.addItem(new Strawberry(0.8), 3);
        cartD.addItem(new Mango(), 2);
        System.out.println("顾客D的消费: " + cartD.calculateTotal() + "元");
    }
}
// 水果抽象类
abstract class Fruit {
    //水果名称
    private String name;
    //水果价格
    private double price;

    public Fruit(String name, double price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    // 计算折扣后价格的抽象方法
    public abstract double getDiscountedPrice(int quantity);
}

// 苹果类
class Apple extends Fruit {
    public Apple() {
        super("苹果", 8.0);
    }

    @Override
    public double getDiscountedPrice(int quantity) {
        return getPrice() * quantity;
    }
}

// 草莓类
class Strawberry extends Fruit {
    //折扣
    private double discountRate;

    public Strawberry(double discountRate) {
        super("草莓", 13.0);
        this.discountRate = discountRate;
    }

    @Override
    public double getDiscountedPrice(int quantity) {
        return getPrice() * quantity * discountRate;
    }
}

// 芒果类
class Mango extends Fruit {
    public Mango() {
        super("芒果", 20.0);
    }

    @Override
    public double getDiscountedPrice(int quantity) {
        return getPrice() * quantity;
    }
}

// 促销策略接口
interface ChuXiaoStrategy {
    double applyChuXiao(double total);
}

// 满减促销策略
class FullDiscountChuXiao implements ChuXiaoStrategy {
    //促销阈值
    private double threshold;
    //促销力度/价格
    private double discount;

    public FullDiscountChuXiao(double threshold, double discount) {
        this.threshold = threshold;
        this.discount = discount;
    }

    @Override
    public double applyChuXiao(double total) {
        return total >= threshold ? total - discount : total;
    }
}

// 无促销策略
class NoChuXiao implements ChuXiaoStrategy {
    @Override
    public double applyChuXiao(double total) {
        return total;
    }
}

// 购物车类
class ShoppingCart {
    //购买水果，数量
    private Map<Fruit, Integer> items = new HashMap<>();
    //销售策略
    private ChuXiaoStrategy ChuXiao;

    public ShoppingCart(ChuXiaoStrategy ChuXiao) {
        this.ChuXiao = ChuXiao;
    }

    /**
     * 添加购买的水果到购物车
     * @param fruit
     * @param quantity
     */
    public void addItem(Fruit fruit, int quantity) {
        items.put(fruit, items.getOrDefault(fruit, 0) + quantity);
    }

    /**
     * 根据促销策略+购买的蔬果进行计算总价格
     * @return
     */
    public double calculateTotal() {
        double subtotal = 0;
        for (Map.Entry<Fruit, Integer> entry : items.entrySet()) {
            subtotal += entry.getKey().getDiscountedPrice(entry.getValue());
        }
        return ChuXiao.applyChuXiao(subtotal);
    }
}
